﻿using mediaControl.ViewModels;
using mediaControl.Vo;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace mediaControl.Views
{
    /// <summary>
    /// ProgramSetting.xaml 的交互逻辑
    /// </summary>
    public partial class ProgramSetting : UserControl
    {
        public ProgramSetting()
        {
            InitializeComponent();
           
        }
       

        private void dataBox_Drop(object sender, DragEventArgs e)
        {
            //Thread.Sleep(50);
            var pos = e.GetPosition(dataBox);
            var result = VisualTreeHelper.HitTest(dataBox, pos);
            if (result == null)
            {
                return;
            }
            //查找元数据
            var sourcePerson = e.Data.GetData(typeof(ProgramItem)) as ProgramItem;
            if (sourcePerson == null)
            {
                return;
            }
            int targetIndex = dataBox.Items.Count-1;
            
            //查找目标数据
            var listBoxItem = Utils.FindVisualParent<ListBoxItem>(result.VisualHit);
            ProgramItem targetPerson = null;
            if (listBoxItem != null)
            {
                targetPerson = listBoxItem.Content as ProgramItem;
                targetIndex = dataBox.Items.IndexOf(targetPerson);
            }
            
            
            if (ReferenceEquals(targetPerson, sourcePerson))
            {
                return;
            }
            int sourceIndex = dataBox.Items.IndexOf(sourcePerson);
            
            ObservableCollection<ProgramItem> voList = new ObservableCollection<ProgramItem>();
            voList = (ObservableCollection<ProgramItem>)dataBox.ItemsSource;
            if (null != targetPerson)
            {
                if (sourceIndex < targetIndex)  //从上面移动到下面
                {

                    voList.Remove(sourcePerson);//删除源
                    voList.Insert(dataBox.Items.IndexOf(targetPerson) + 1, sourcePerson);


                }
                else if (sourceIndex > targetIndex)
                {
                    voList.Remove(sourcePerson);//删除源
                    voList.Insert(dataBox.Items.IndexOf(targetPerson), sourcePerson);
                }
            }
            else {
                //voList.Remove(sourcePerson);
                //voList.Insert(targetIndex, sourcePerson);
                return;
            }
            dataBox.ItemsSource = voList;
            if (DataContext is ProgramSettingViewModel viewModel)
            {
                viewModel.TempList = voList;
            }
        }

        private void dataBox_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            DependencyObject depObj = (DependencyObject)e.OriginalSource;
            if (depObj is CheckBox)
            {
                e.Handled = false;
                return;
            }
            while (depObj != null && !(depObj is TextBox))
            {
                depObj = VisualTreeHelper.GetParent(depObj);
            }
            if (depObj is TextBox)
            {
                e.Handled = false;
                return;
            }
            while (depObj != null && !(depObj is ComboBox))
            {
                depObj = VisualTreeHelper.GetParent(depObj);
            }
            if (depObj is ComboBox)
            {
                e.Handled = false;
                return;
            }
            while (depObj != null && !(depObj is CheckBox))
            {
                depObj = VisualTreeHelper.GetParent(depObj);
            }
            if (depObj is CheckBox)
            {
                e.Handled = false;
                return;
            }
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                if (depObj is CheckBox)
                {
                    e.Handled = false;
                    return;
                }
                while (depObj != null && !(depObj is CheckBox))
                {
                    depObj = VisualTreeHelper.GetParent(depObj);
                }
                
                while (depObj != null && !(depObj is TextBox))
                {
                    depObj = VisualTreeHelper.GetParent(depObj);
                }
                if (depObj is TextBox)
                {
                    e.Handled = false;
                    return;
                } 
                while (depObj != null && !(depObj is ComboBox))
                {
                    depObj = VisualTreeHelper.GetParent(depObj);
                }
                if (depObj is ComboBox)
                {
                    e.Handled = true;
                    return;
                }
                var pos = e.GetPosition(dataBox);
                HitTestResult result = VisualTreeHelper.HitTest(dataBox, pos);
                if (result == null)
                {
                    return;
                }
                var listBoxItem = Utils.FindVisualParent<ListBoxItem>(result.VisualHit);
                if (listBoxItem == null || listBoxItem.Content != dataBox.SelectedItem)
                {
                    return;
                }
                DataObject dataObj = new DataObject(listBoxItem.Content as ProgramItem);
                DragDrop.DoDragDrop(dataBox, dataObj, DragDropEffects.Move);
                
            }
        }

        internal static class Utils
        {
            //根据子元素查找父元素
            public static T FindVisualParent<T>(DependencyObject obj) where T : class
            {
                while (obj != null)
                {
                    if (obj is T)
                        return obj as T;

                    obj = VisualTreeHelper.GetParent(obj);
                }
                return null;
            }
        }

        private void UserControl_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effects = DragDropEffects.Link;
            }
            else
            {
                e.Effects = DragDropEffects.None;
            }
        }

        private void checkGroup_Click(object sender, RoutedEventArgs e)
        {
            string flag = playFlag.Text;
            if ("True".Equals(flag))
            {
                CheckBox checkBox = sender as CheckBox;
                checkBox.IsChecked = false;
                MessageBox.Show("有节目播放中，不能修改！");
                e.Handled = true;
                return;
            }
        }

        private void ComboBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            string flag = playFlag.Text;
            if ("True".Equals(flag))
            {
                MessageBox.Show("有节目播放中，不能修改！");
                e.Handled = true;
                return;
            }
        }
    }

}
